package com.apache.tomcat;

import com.apache.client.servlet.RandCodeImageServlet;
import com.apache.passport.common.JwtClientFilter;
import com.apache.rpc.StartSocketPortListener;
import com.apache.rpc.common.RpcUtil;
import com.apache.tools.StrUtil;
import com.apache.uct.common.filter.LicenseFilter;
import com.apache.portal.common.restfull.ResultFullFilter;
import com.apache.portal.contorller.UploadServlet;
import com.apache.uct.common.filter.UctClientFilter;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * FilterConfiguration<br>
 * @Author IUS platform administrators create generation
 * @Date 2020-1-31 13:51:04
 */
@Configuration
public class FilterConfiguration extends WebMvcConfigurerAdapter {
    private Logger log = Logger.getLogger(getClass());
    @Autowired
    private Environment env;

    //解决跨域请求
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET", "POST", "OPTIONS", "PUT")
            .allowedHeaders("Content-Type", "X-Requested-With", "accept", "Origin", "Access-Control-Request-Method",
                "Access-Control-Request-Headers")
            .exposedHeaders("Access-Control-Allow-Origin", "Access-Control-Allow-Credentials")
            .allowCredentials(true).maxAge(3600);
    }

    @Bean
    public StartSocketPortListener startSocketPortListener() {
        String socketPort = StrUtil.doNull(env.getProperty("rpc.service.port"), "20808");
        String rpcVersion = env.getProperty("rpc.version");
        log.info("rpc.service.port->"+socketPort);
        log.info("rpc.version->"+rpcVersion);
        StartSocketPortListener listener = new StartSocketPortListener(socketPort);
        if (StrUtil.isNotNull(rpcVersion)) {
            RpcUtil.getInstance().setVersion(rpcVersion);
        }
        return listener;
    }

    @Bean
    public FilterRegistrationBean encodingFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new CharacterEncodingFilter());
        registration.addUrlPatterns("/*");
        registration.addInitParameter("encoding", "UTF-8");
        registration.addInitParameter("forceEncoding", "true");
        registration.setName("encodingFilter");
        registration.setOrder(1);
        return registration;
    }

    @Bean
    public FilterRegistrationBean defaultBaseSecurityFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new LicenseFilter());
        registration.addUrlPatterns("/*");
        registration.setName("LicenseFilter");
        registration.setOrder(2);
        return registration;
    }

    //注入其他请求地址Filter
    @Bean
    public FilterRegistrationBean resultFullFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new ResultFullFilter());
        registration.addUrlPatterns("/common/coding/*","/trans/template/*","/res/uct/*");//多个英文逗号隔开
        registration.setName("resultFullFilter");
        registration.addInitParameter("servletPackage", "com.apache.portal");
        registration.setOrder(3);
        return registration;
    }

    @Bean
    public FilterRegistrationBean ssoClientFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new JwtClientFilter());
        registration.addUrlPatterns("/*", "/logout", "/ssb");
        registration.setName("ssoClientFilter");
        registration.addInitParameter("sysCode", "ssb");//指定白名单（注意配置ssb_white.xml）
        registration.setOrder(4);
        return registration;
    }

    @Bean
    public FilterRegistrationBean uctClientFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new UctClientFilter());
        registration.addUrlPatterns("/*");
        registration.setName("uctClientFilter");
        registration.addInitParameter("sysCode", "ssb");//指定白名单（注意配置ssb_white.xml）
        registration.setOrder(5);
        return registration;
    }

    //注入文件上传servlet
    @Bean
    public ServletRegistrationBean getServletRegistrationBean() {  //一定要返回ServletRegistrationBean
        ServletRegistrationBean bean = new ServletRegistrationBean(new UploadServlet());//放入自己的Servlet对象实例
        bean.addUrlMappings("/uploadServlet");  //访问路径值
        return bean;
    }

    //注入文件验证码servlet
    @Bean
    public ServletRegistrationBean getRandCodeImageServlet() {  //一定要返回ServletRegistrationBean
        ServletRegistrationBean bean = new ServletRegistrationBean(new RandCodeImageServlet(), "/web/CheckRandCode");
        return bean;
    }

}
